inp = int(input("Введите четырехзначное число: "))
nums = []
success_nums = [0]
if inp < 0:
    inp = -(inp)
for i in range(2, inp+1):
    deliteli = 0
    for j in range(1, i+1):
        if i % j == 0:
            deliteli += 1
    if deliteli == 2:
        nums.append(i)  
inp = str(inp)   
for i in nums:
    if str(i) in inp:
        success_nums.append(i)
print(max(success_nums))    